package com.qwe.graduation.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qwe.graduation.entity.Video;
import com.qwe.graduation.utils.exception.Constants;
import com.qwe.graduation.utils.exception.ServiceException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.qwe.graduation.utils.exception.Result;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import com.qwe.graduation.service.IMessageService;
import com.qwe.graduation.entity.Message;


/**
 * <p>
 * 资讯表 前端控制器
 * </p>
 *
 * @author 林卓锋
 * @since 2023-01-18
 */
@RestController
@RequestMapping("/message")
public class MessageController {

    @Resource
    private IMessageService messageService;

    //新增或更改
    @PostMapping("/save")
    public Result save(@RequestBody Message message) {
        if (message.getId() == null) {
            message.setCreateTime(new Date());
        }
        message.setUpdateTime(new Date());
        if (StringUtils.isBlank(message.getHeadline())) {
            throw new ServiceException(Constants.CODE_400, "资讯标题不能为空！");
        }
        if (StringUtils.isBlank(message.getMessage())) {
            throw new ServiceException(Constants.CODE_400, "资讯内容不能为空！");
        }
        if (StringUtils.isBlank(message.getModule())) {
            throw new ServiceException(Constants.CODE_400, "资讯类型不能为空！");
        }
        message.setUpdateTime(new Date());
        return Result.success(messageService.saveOrUpdate(message));
    }

    //删除
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable String id) {
        return Result.success(messageService.removeById(id));
    }

    //查找全部
    @GetMapping
    public List<Message> findAll() {
        return messageService.list();
    }

    //根据id查询
    @GetMapping("/{id}")
    public List<Message> findOne(@PathVariable String id) {
        return messageService.list();
    }

    //批量删除
    @PostMapping("/delete/batch")
    public Result deleteBeatch(@RequestBody List<String> ids) {
        return Result.success(messageService.removeByIds(ids));
    }

    //分页
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "",required = false) String id,
                           @RequestParam(defaultValue = "",required = false) String module,
                           @RequestParam(defaultValue = "",required = false) String headline) {
        QueryWrapper<Message> queryWrapper =new QueryWrapper<>();
        queryWrapper.orderByDesc("update_time")
                .like("id", id)
                .like("headline",headline)
                .like("module",module);
        Page<Message> page = messageService.page(new Page<>(pageNum, pageSize),queryWrapper);
        return Result.success(page);
    }

}

